package com.fullstack.controller;

import com.databases.dao.CartMapper;
import com.databases.dao.ClothesMapper;
import com.fullstack.bean.Cart;
import com.fullstack.bean.Cart_Clothes;
import com.fullstack.bean.Clothes;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class CartController {

    public void paycartclothes(HttpServletRequest request){
        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象
            //添加购物车
            CartMapper mapper = session.getMapper(CartMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            mapper.paycartclothes(request.getParameter("name"));


            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void changecartclothes(HttpServletRequest request){
        Cart cart = new Cart();
        cart.setId(Integer.parseInt(request.getParameter("clothes_id")));
        cart.setNum(Integer.parseInt(request.getParameter("num")));
        cart.setUsername(request.getParameter("name"));
        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象
            //添加购物车
            CartMapper mapper = session.getMapper(CartMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            mapper.updatetocart(cart);

            mapper.updatetoclothes(cart);


            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<Cart_Clothes> selectcartclothes(HttpServletRequest request){
        List<Cart_Clothes> list = null;
        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象
            //添加购物车
            CartMapper mapper = session.getMapper(CartMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            list = mapper.selectcartclothes(request.getParameter("name"));
//            for(Cart_Clothes i :list){
//                System.out.println(i.toString());
//            }
            //返回值
//            Cart_Clothes{clothes_id=90023, clothes_type='女', clothes_brand='SJYP', clothes_info='Felix the Cat 联乘系列 饰口袋牛仔夹克',
//            clothes_money=3799, clothes_num=227, clothes_src='c2.jpg', clothes_cart_num=0, cart_username='liu705195178', cart_num=7,
//            cart_cart_id=10003}


            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


        return  list;

    }

    public void addtocart(HttpServletRequest request){

        Cart cart = new Cart();
        cart.setId(Integer.parseInt(request.getParameter("id")));
        cart.setNum(Integer.parseInt(request.getParameter("cart_num")));
        cart.setUsername(request.getParameter("name"));
        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象
            //添加购物车
            CartMapper mapper = session.getMapper(CartMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            //首先判断
            int selecttocart = mapper.selecttocart(cart);
            System.out.println(selecttocart);
            if(selecttocart == 0){
                mapper.addtocart(cart);
            }
            else{
                mapper.updatetocart(cart);
            }
            //减少库存
            ClothesMapper mapper1 = session.getMapper(ClothesMapper.class);

            mapper1.updateclothes_whencartadd(cart);

            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
